﻿// Fill out your copyright notice in the Description page of Project Settings.


#include "UMG/PauseWidget.h"

#include "Components/AudioComponent.h"
#include "Components/Button.h"
#include "GameMode/CombatGameMode.h"
#include "Kismet/GameplayStatics.h"

bool UPauseWidget::Initialize()
{
	if (!Super::Initialize())
	{
		return false;
	}
	ContinueBtn->OnClicked.AddDynamic(this, &UPauseWidget::ContinueBtnFunc);
	RestartBtn->OnClicked.AddDynamic(this, &UPauseWidget::RestartBtnFunc);
	BackBtn->OnClicked.AddDynamic(this, &UPauseWidget::BackBtnFunc);

	MusicCheck->OnCheckStateChanged.AddDynamic(this, &UPauseWidget::OnCheckBoxComponentStateChanged);
	MusicSlider->OnValueChanged.AddDynamic(this, &UPauseWidget::OnFloatValueChangedEvent);
	return true;
}

void UPauseWidget::ContinueBtnFunc()
{
	ACombatGameMode* GameMode = Cast<ACombatGameMode>(UGameplayStatics::GetGameMode(GetWorld()));
	if (GameMode)
	{
		GameMode->BackToMainUI();
	}
}

void UPauseWidget::RestartBtnFunc()
{
	// player恢复操作
	APlayerCharacter* PlayerPawn = Cast<APlayerCharacter>(UGameplayStatics::GetPlayerCharacter(GetWorld(), 0));
	if (PlayerPawn)
	{
		PlayerPawn->StartOperation();
	}
	UGameplayStatics::OpenLevel(GetWorld(), FName("SunTemple"));
}

void UPauseWidget::BackBtnFunc()
{
	APlayerCharacter* PlayerPawn = Cast<APlayerCharacter>(UGameplayStatics::GetPlayerCharacter(GetWorld(), 0));
	if (PlayerPawn)
	{
		PlayerPawn->StopOperation();
	}
	UGameplayStatics::OpenLevel(GetWorld(),FName("StartMenu"));
}

void UPauseWidget::OnCheckBoxComponentStateChanged(bool bIsChecked)
{
	ACombatGameMode* GameMode = Cast<ACombatGameMode>(UGameplayStatics::GetGameMode(GetWorld()));
	if (GameMode)
	{
		if (GameMode->AudioActor)
		{
			GameMode->AudioActor->AudioComponent->SetPaused(!bIsChecked);
		}
	}
}

void UPauseWidget::OnFloatValueChangedEvent(float Value)
{
	ACombatGameMode* GameMode = Cast<ACombatGameMode>(UGameplayStatics::GetGameMode(GetWorld()));
	if (GameMode)
	{
		if (GameMode->AudioActor)
		{
			GameMode->AudioActor->AudioComponent->SetVolumeMultiplier(Value);
		}
	}
}
